/**
 * @class   wosh::security::AlarmState
 * @brief   The class hosts current state of the Alarm system and its history.
 *          
 *
 *
 ****************************************************************************
 * @version $Id: AlarmState.h 2863 2010-08-07 04:28:58Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Security_AlarmState_H__
 #define __WOSH_Security_AlarmState_H__

 #include <woshDefs.h>
 #include <core/Object.h>
 #include <core/Containers.h>
 #include <core/Serialization.h>

 #include <framework/security/AlarmEvent.h>
 #include <framework/security/AlarmListeners.h>



namespace wosh {
 namespace security {

class AlarmState : public Object
 {
	WOSH_SERIALIZABLE(wosh::security::AlarmState)
	WOSH_COMPARE_RTTI(wosh::security::AlarmState)

	typedef ListT<wosh::security::AlarmEvent*> tAlarmEventList;


	public:
		/**
		 * @brief  Empty constructor.
		 */
		AlarmState() : Object() {
			Object::setName("AlarmState");

			this->listener = NULL;
			this->cLevel = 0;

			this->tsArmed = 0;
			this->tsDisarmed = 0;

			this->tsLastTrigger = 0;
			this->tsLastEffect = 0;
		 }
		/**
		 * @brief  Copy constructor.
		 * @param m [in] the source object to be copied
		 */
		AlarmState( const AlarmState& m ) : Object(m) {
			this->listener = NULL;

			this->cLevel = m.cLevel;

			this->tsArmed = m.tsArmed;
			this->tsDisarmed = m.tsDisarmed;

			this->tsLastTrigger = m.tsLastTrigger;
			this->tsLastEffect = m.tsLastEffect;
		 }
		/**
		 * @brief  Empty destructor.
		 */
		virtual ~AlarmState() { }



	public:

		void raiseEvent( AlarmEvent* alarm_event )
		 {
			this->history.transactionBeginWrite();
			this->history.push_back(alarm_event);

			if ( this->listener != NULL )
				this->listener->raised_AlarmEvent( alarm_event, this );

			this->history.transactionEnd();
		 }

//@}
/** @name Setters
 * @{
 ******************************************************************************/
	public:
		void setListener(IAlarmStateListener* listenerPtr ) { this->listener = listenerPtr; }

//@}
/** @name Getters
 * @{
 ******************************************************************************/
	public:
		long getLevel() const						{ return this->cLevel; }

		long getTimeStampArmed() const				{ return this->tsArmed; }
		long getTimeStampDisarmed() const			{ return this->tsDisarmed; }

		long getTimeStampLastTrigger() const		{ return this->tsLastTrigger; }
		long getTimeStampLastEffect() const			{ return this->tsLastEffect; }


		AlarmEvent* getLastEvent() const {
			MutexLockerRead mL(this->history.getMutex());
			if ( this->history.size() == 0 )
				return NULL;
			return this->history.backItem()->clone();
		 }

		const tAlarmEventList getEvents() const		{ return this->history; }
		tAlarmEventList getEvents()					{ return this->history; }

		IAlarmStateListener* getListener()			{ return this->listener; }

//@}
/** @name Serialization interface
 * @{
 ******************************************************************************/
	public:
		virtual std::string toString() const		{ return ""; }

//@}
	protected:

		long cLevel;		///< global alarm level

		long tsArmed;		///< when alarm was armed (epoc time)
		long tsDisarmed;	///< when alarm was disarmed (epoc time) 

		long tsLastTrigger;	///< when alarm had last validated trigger (epoc time) 
		long tsLastEffect;	///< when alarm had an effect (fact, notification,..) (epoc time) 

		mutable tAlarmEventList history;	///< previous events


		IAlarmStateListener* listener;		///< callbacks



}; // class def

 }; // namespace security
}; // namespace wosh

#endif //__WOSH_Security_AlarmState_H__
